﻿/**************************************************************************************
 * DocumentSocialHelper.cs
 * An utility class to manage tags and comments on  sharepoint items
 * Jaime Alberto Jaramillo Zapata (jjaramillo.informatica-tecnologia.com)
 * Created on 03/02/2012.
 * Last update on 03/02/2012.
 **************************************************************************************/

using System;
using System.Threading;
using Microsoft.Office.Server.SocialData;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace MS.InformationManagement.Support
{
    /// <summary>
    /// An utility class to manage tags and comments on  sharepoint items
    /// </summary>
    public class DocumentSocialHelper
    {
        /// <summary>
        /// Tags the document with the specified text.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="documentUrl">The target document's relative URL.</param>
        /// <param name="web">The Sharepoint foundation website where the document is being stored.</param>
        /// <returns>The social tag object.</returns>
        public static SocialTag TagDocument(string tag, string documentUrl, SPWeb web)
        {
            SPUserToken currentUserToken = web.CurrentUser.UserToken;
            SocialTag socialTag = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    using (SPSite site = new SPSite(web.Site.ID, currentUserToken))
                    {
                        using (SPWeb overridenSecurityWeb = site.OpenWeb())
                        {
        
                            SPFile targetDocument = overridenSecurityWeb.GetFile(documentUrl);
                            SocialTagManager tagManager = new SocialTagManager(SPServiceContext.GetContext(site));
                            TaxonomySession taxonomySession = tagManager.TaxonomySession;
                            TermStore termStore = taxonomySession.DefaultKeywordsTermStore;
                            Term term = termStore.KeywordsTermSet.CreateTerm(tag, Thread.CurrentThread.CurrentUICulture.LCID);
                            termStore.CommitAll();
                            socialTag = tagManager.AddTag(new Uri(targetDocument.ServerRelativeUrl), term, targetDocument.Title);
                        }

                    }

                }
                catch (Exception ex)
                {
                    //TODO: manage exception
                    throw;
                }
            });


            return socialTag;
        }

        /// <summary>
        /// Adds a note (comment) to a document.
        /// </summary>
        /// <param name="comment">The comment.</param>
        /// <param name="documentUrl">The document's relative URL.</param>
        /// <param name="web">The Sharepoint foundation website where the document is being stored.</param>
        /// <param name="isHighPriority">if set to <c>true</c> [is high priority].</param>
        /// <returns>The social comment object</returns>
        public static SocialComment AddNoteToADocument(string comment, string documentUrl, SPWeb web, bool isHighPriority)
        {
            SocialComment socialComment = null;
            try
            {
                SPFile targetDocument = web.GetFile(documentUrl);
                SocialCommentManager commentManager = new SocialCommentManager(SPServiceContext.Current);
                socialComment = commentManager.AddComment(new Uri(targetDocument.ServerRelativeUrl), comment, isHighPriority, targetDocument.Title);
            }
            catch (Exception ex)
            {
                //TODO: manage exception.
                throw;
            }
            return socialComment;
        }

    }
}
